package com.fanbuchi.fukua.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.fanbuchi.fukua.R;
import com.fanbuchi.fukua.entity.MsgEntity;
import com.fanbuchi.fukua.entity.enums.Actions;
import com.fanbuchi.fukua.entity.event.ClickEvent;
import com.fanbuchi.fukua.entity.event.LongClickEvent;

import java.util.List;

import de.greenrobot.event.EventBus;

/**
 * Created by Administrator on 2015/7/16.
 */
public class MsgAdapter extends RecyclerView.Adapter<MsgAdapter.ViewHolder> implements  View.OnClickListener,View.OnLongClickListener{
    List<MsgEntity> msgEntities;
    public MsgAdapter( List<MsgEntity> msgEntities){
        this.msgEntities=msgEntities;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.activity_msg_item, parent, false);

        ViewHolder vh = new ViewHolder(v);
        return vh;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        MsgEntity msgEntity= msgEntities.get(position);
        holder.getTitle().setText(msgEntity.getTitle());
        holder.getContent().setText(msgEntity.getContent());

        holder.itemView.setTag(msgEntity);
        holder.itemView.setOnClickListener(this);
        holder.itemView.setOnLongClickListener(this);
    }

    @Override
    public int getItemCount() {
        return msgEntities.size();
    }

    @Override
    public void onClick(View view) {
        MsgEntity msg=(MsgEntity) view.getTag();
        EventBus.getDefault().post(new ClickEvent(Actions.action_click,msg));
    }

    @Override
    public boolean onLongClick(View view) {
        MsgEntity msg=(MsgEntity) view.getTag();
        EventBus.getDefault().post(new LongClickEvent(Actions.action_longclick,msg));
        return true;
    }






    public static class ViewHolder extends RecyclerView.ViewHolder {
        private TextView title,content;

        public ViewHolder(View itemView) {
            super(itemView);
            title=(TextView)itemView.findViewById(R.id.title);
            content=(TextView)itemView.findViewById(R.id.content);
        }

        public TextView getTitle() {
            return title;
        }
        public TextView getContent() {
            return content;
        }

    }

}
